package com.caterina.activities;
import java.awt.Container;
import java.util.Collection;

import javax.swing.JScrollPane;

import person.User;
import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.composite.MUserProfile;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.resources.CWords;

import controllers.ManagerTask.TaskType;
import controllers.Permission;
import controllers.PermissionList;

public class UserInfoActivity extends CActivity implements ModuleChangedListener{
	public UserInfoActivity(Container parent) {
		super("USER_INFO",parent);
	}
	@Override
	public void onStart() {
		this.putTask(TaskType.USER_CURRENT);
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onData(TaskType task, Object data) {
		switch (task){
			case USER_CURRENT:{
				this.user=(User)data;
				this.putTask(TaskType.USER_PERMISSIONS);
				break;
			}
			case USER_PERMISSIONS:{
				this.permissions=new PermissionList((Collection<Permission>) data);
				this.renderMainPanel();
				break;
			}
			case USER_MODIFY_PASSWORD:{
				this.showCOPtionPane(MessageType.NORMAL_OK, CWords.getWord("MODIFY_PASSWORD_DONE"));
				this.renderMainPanel();
			}
			default:break;
		}
	}

	@Override
	public void onError(TaskType task, Exception e) {
		switch (task){
		case USER_MODIFY_PASSWORD:{
			this.showCOPtionPane(MessageType.ERROR, CWords.getWord("MODIFY_PASSWORD_ERROR"));
		}
		default:break;
	}
	}
	@Override
	public void onModuleChange(Module<?> mod) {
		
	}
	private void createButtons(){
		this.passwordModify=new CButton(CWords.getWord("USER_PASSWORD_MODIFY_BUTTON")) {		
			@Override
			public void onFocus() {}
			
			@Override
			public void onAction() {
				putTask(TaskType.USER_MODIFY_PASSWORD, user,userP.getContent());
			}
			private static final long serialVersionUID = 1L;
		};
		userP.addButtons(passwordModify);
	}
	private void renderMainPanel(){
		this.userP=new MUserProfile(null, user, permissions, "USER_PROFILE", "USER_PROFILE_DESC", ViewType.READ_ONLY);
		this.createButtons();
		this.add(new JScrollPane(userP.getPanel()));
	}
	private CButton passwordModify;
	private PermissionList permissions;
	private MUserProfile userP;
	private User user;
	private static final long serialVersionUID = 1L;
}
